<template>
    <BaseContainer>
        <aplid-base-crud ref="crud" class="w-100" v-bind="baseConfig">
            <template #trainingName ="{row}">
                <el-tooltip
                    v-if="row.daysLeftToExpire"
                    effect="dark"
                    :content="judgeTiop(row.daysLeftToExpire)"
                    placement="bottom"
                >
                <span style="color:red">{{row.trainingName}}</span>
                </el-tooltip>
                <span v-else>{{row.trainingName}}</span>
           </template>
            <template #editDialog="{row}">
                <EditForm :nowRowData="row" @refresh="handleRefresh"></EditForm>
            </template>
            <template #callbackDialog="{row}">
                <el-form>
                    <el-form-item label="驳回意见" prop="rejectReason">
                        <el-input :disabled="true" :value="row.approvalRemark" type="textarea" :rows="4" placeholder="请输入驳回意见" />
                    </el-form-item>
                </el-form>
            </template>
            <template #detailDialog="{row}">
                <!-- 民政 -->
                <div v-if="userType=='govUser'">
                    <DetailForm :nowRowData="row"></DetailForm>
                    <div class="btn_part" v-if="(row.approvalStatus==1)&&userInfo.id==row.createUserId">
                        <el-button type="primary" @click="surePass(row)">通过</el-button>
                        <el-button type="danger" @click="openRejectShow(row)">驳回</el-button>
                    </div>
                </div>
                <!-- 服务商 -->
                <div v-if="userType=='providerUser'">
                    <DetailForm :nowRowData="row"></DetailForm>
                </div>
            </template>
        </aplid-base-crud>
        <!-- 驳回弹窗 -->
         <AplidDialog  title="驳回意见" :width="'60%'" v-model="rejectShow">
            <el-form>
                <el-form-item label="驳回意见" prop="rejectReason">
                    <el-input v-model="rejectReason" type="textarea" :rows="4" placeholder="请输入驳回意见" />
                </el-form-item>
            </el-form>
            <div class="btn_part">
                <el-button type="" @click="rejectShow=false">取消</el-button>
                <el-button type="primary" @click="sureReject">确定</el-button>
            </div>
        </AplidDialog>
    </BaseContainer>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted} from 'vue'
import { baseConfig,editFormConfig} from './config'
import DetailForm from './detailForm/index.vue'
import AddFrom from './addForm/index.vue'
import EditForm from './editForm/index.vue'
import { getStore } from '@/utils/store'
import apiMod from '@/apiMod/index'
import {cloneDeep} from 'lodash-es'

let rejectReason = ref('')
let userInfo = getStore('userInfo')
let rejectShow = ref(false)
// 判断是服务商还是民政
let userType = ref('govUser')
if(userInfo.classifyId==5){
	// 服务商
	userType.value = 'providerUser'
}else if(userInfo.classifyId==3){
	//民政用户
	userType.value = 'govUser'
}

let crud = ref()
  //  处理刷新
  const handleRefresh = (query?: any) => {
      nextTick(() => {
          crud.value.refreshChange(query)
      })
  }
  // 驳回
let copyRejectData:any = ref({})
const openRejectShow = (row) =>{
    rejectShow.value = true
    copyRejectData.value = cloneDeep(row)
}
const surePass = (row) =>{
    let prarms = {
        ...row,
        approvalStatus:2,
        approvalStatusLabel:'审批通过',
    }
    apiMod.view_bscTrainingManagement_edit(prarms).then(res=>{
        if(res.code == 0){
            rejectShow.value = false
            handleRefresh()
        }
    })
}
// 确认驳回
const sureReject = () =>{
    let prarms = {
        ...copyRejectData.value,
        approvalStatus:3,
        approvalStatusLabel:'审批驳回',
        approvalRemark:rejectReason.value
    }
    apiMod.view_bscTrainingManagement_edit(prarms).then(res=>{
        if(res.code == 0){
            rejectShow.value = false
            handleRefresh()
        }
    })
}
const judgeTiop = (data) =>{
    if(Number(data)>0){
        return `距离过期剩余${data}天`
    }else if(Number(data)<0){
        return `已过期${Math.abs(data)}天`
    } else if(Number(data) == 0){
        return '今天过期'
    } else{
        return '已过期'
    }
  }


</script>
<style scoped lang="scss">
.btn_part{
    margin-top: 20px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}
</style>